package com.codeoss.framework.impexp;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.converter.IConverter;
import com.codeoss.framework.converter.IConverterStore;
import com.codeoss.framework.data.metadata.Column;
import com.codeoss.framework.data.metadata.Query;
import com.codeoss.framework.data.metadata.Restrictions;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.metadata.TableData;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.view.datasource.IDataSource;
import com.codeoss.framework.view.datasource.store.IDataSourceStore;

@Service
public class PropertiesExport implements IPropertiesExport {
	@Autowired
	private IDataSourceStore dataSourceStore;
	@Autowired
	private IConverterStore converterStore;
	@Autowired
	private ITableStore tableStore;
	@Override
	public void exportProperties(YunContext yunContext, Table table, Query query, OutputStream os) throws IOException{
		// TODO Auto-generated method stub
		Properties p=new Properties();
		IDataSource dataSource=dataSourceStore.getDataSource(table.getAccessType());
		TableData tableData=dataSource.select(yunContext, table, query);
		if(tableData.getTotal()>0) {
			Map<String,Object> dataMap=tableData.getData().get(0);
			List<Column> columnList=table.getAllColumnList();
			for (int i = 0; i < columnList.size(); i++) {
				Column column=columnList.get(i);
				if(column.isExportColumn()) {
					Object value=dataMap.get(column.getColumnName());
					if(value==null) {
						p.put(column.getColumnName(), "");
					}else {
						if(column.isParentColumn()) {
							DataSourceConfig dataSourceConfig=column.getLookupInfo().getDataSourceConfig();
							Table parentTable=tableStore.get(dataSourceConfig.getTableName());
							IDataSource parentDataSource=dataSourceStore.getDataSource(parentTable.getAccessType());
							Query parentQuery=new Query();
							parentQuery.add(Restrictions.eq(parentTable.getPrimaryKeyColumn().getColumnName(), value));
							TableData parentTableData=parentDataSource.select(yunContext, parentTable, parentQuery);
							if(parentTableData.getTotal()>0) {
								Map<String,Object> parentDataMap=parentTableData.getData().get(0);
								String parentCode=(String)parentDataMap.get(parentTable.getCodeColumn().getColumnName());
								p.put(column.getColumnName(),parentCode);
							}
						}else if(column.getLookupInfo()!=null && column.getLookupInfo().getDataSourceConfig()!=null) {
							DataSourceConfig dataSourceConfig=column.getLookupInfo().getDataSourceConfig();
							Table lookupTable=tableStore.get(dataSourceConfig.getTableName());
							IDataSource lookupDataSource=dataSourceStore.getDataSource(lookupTable.getAccessType());
							Query lookupQuery=new Query();
							lookupQuery.add(Restrictions.eq(lookupTable.getPrimaryKeyColumn().getColumnName(), value));
							TableData lookupTableData=lookupDataSource.select(yunContext, lookupTable, lookupQuery);
							if(lookupTableData.getTotal()>0) {
								Map<String,Object> lookupDataMap=lookupTableData.getData().get(0);
								String lookupCode=(String)lookupDataMap.get(lookupTable.getCodeColumn().getColumnName());
								p.put(column.getColumnName(),lookupCode);
							}
						}else {
							IConverter converter=converterStore.getConverter(column.getDataType());
							String sValue=converter.convertObjectToString(value);
							p.put(column.getColumnName(),sValue);
						}
					}
				}
			}
			if(table.isTreeTable()) {
				Object parentId=dataMap.get("parent_id");
				if(parentId!=null) {
					Query parentQuery=new Query();
					parentQuery.add(Restrictions.eq(table.getPrimaryKeyColumn().getColumnName(), parentId));
					TableData parentTableData=dataSource.select(yunContext, table, parentQuery);
					if(parentTableData.getTotal()>0) {
						Map<String,Object> parentDataMap=parentTableData.getData().get(0);
						p.put("_parent_code",parentDataMap.get(table.getCodeColumn().getColumnName()));
					}
				}
			}
		}
		p.store(os, "");
	}

}
